#!/bin/bash

# 检查 nvm 是否安装
# if ! command -v nvm &> /dev/null; then
#     echo "Error: nvm is not installed."
#     echo "To install nvm, use the following commands:"
#     echo "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash"
#     echo "Then, restart your terminal or run:"
#     echo "source ~/.nvm/nvm.sh"
#     exit 1
# fi

nvm install 20.2.0
echo "nvm is installed"
nvm use 20.2.0


ROOT_DIR=$(pwd)
echo "ROOT_DIR:  $ROOT_DIR"
# 设置编译环境变量，确保所有依赖项可解析
export GO111MODULE=on

rm -rf dist/ dcs/target/  dcs/dist  vtools/dist
#mkdir -p dist dist/vdam_linux_arm64 dist/vdam_linux_amd64 dist/vdam_darwin_amd64 dist/vdam_darwin_arm64 dist/vdam_windows_amd64
#mkdir -p dist/vdam_linux_arm64/configs dist/vdam_linux_amd64/configs  dist/vdam_darwin_amd64/configs dist/vdam_darwin_arm64/configs  dist/vdam_windows_amd64/configs
#mkdir -p dist/vdam_linux_arm64/sbin dist/vdam_linux_amd64/sbin dist/vdam_darwin_amd64/sbin dist/vdam_darwin_arm64/sbin  dist/vdam_windows_amd64/sbin

mkdir -p dist/vdam_linux_{arm64,amd64}/{configs,sbin}


# 一、编译vtool前端
PACKAGE_DIR="$ROOT_DIR/vtools/vdam"
cd $PACKAGE_DIR
echo "1.build............................................vtools frontend"
# 获取 git 分支
branch=$(git rev-parse --abbrev-ref HEAD)
# 获取 commitId
commitId=$(git rev-parse --short HEAD)
# 获取编译日期
buildDate=$(date '+%Y-%m-%d %H:%M:%S')
buildDate1=$(date '+%Y%m%d_%H%M%S')
echo "$branch $commitId $buildDate"
# 更新 AboutView.vue 文件中的分支、commitId 和编译日期信息
sed -i'' -e "s/git分支: [a-zA-Z0-9\-]\+/git分支: $branch/" \
             -e "s/commitId: [a-zA-Z0-9]\+/commitId: $commitId/" \
             -e "s/编译日期: [0-9]\+-[0-9]\+-[0-9]\+ [0-9]\+:[0-9]\+:[0-9]\+/编译日期: $buildDate/" \
             ${PACKAGE_DIR}/src/views/AboutView.vue

#
npm install
npm run build

rm -rf ../src/apps/dashboard/public
mkdir -p ../src/apps/dashboard/public
cp -rf ./dist/* ../src/apps/dashboard/public/


# 二、编译vtool后端
PACKAGE_DIR="$ROOT_DIR/vtools/src"
echo "2.build............................................vtools backend"
mkdir -p ${PACKAGE_DIR}/../dist
cd $PACKAGE_DIR
go mod tidy
echo "build and copy apps "
# 编译为 ARM Linux 平台 、 编译为 x86 Linux 平台、为 macOS x86_64 架构编译
echo "Building for ARM Linux...""$PACKAGE_DIR/../dist/vtools-arm"
env GOOS=linux GOARCH=arm64 go build -o "$PACKAGE_DIR/../dist/vtools-linux-arm64"
echo "Building for x86 Linux...""$PACKAGE_DIR/../dist/vtools-amd64"
env GOOS=linux GOARCH=amd64 go build -o "$PACKAGE_DIR/../dist/vtools-linux-amd64"
#echo "Building for macOS amd64..."
#env GOOS=darwin GOARCH=amd64 go build -o "$PACKAGE_DIR/../dist/vtools-darwin-amd64"
#echo "Building for macOS arm64..."
#env GOOS=darwin GOARCH=arm64 go build -o "$PACKAGE_DIR/../dist/vtools-darwin-arm64"
#echo "Building for win x86_64..."
#env GOOS=windows GOARCH=amd64 go build -o "$PACKAGE_DIR/../dist/vtools-windows-amd64.exe"
cp $PACKAGE_DIR/../dist/vtools-linux-arm64  $ROOT_DIR/dist/vdam_linux_arm64/vtools
cp $PACKAGE_DIR/../dist/vtools-linux-amd64  $ROOT_DIR/dist/vdam_linux_amd64/vtools
#cp $PACKAGE_DIR/../dist/vtools-darwin-amd64  $ROOT_DIR/dist/vdam_darwin_amd64/vtools
#cp $PACKAGE_DIR/../dist/vtools-darwin-arm64  $ROOT_DIR/dist/vdam_darwin_arm64/vtools
#cp $PACKAGE_DIR/../dist/vtools-windows-amd64.exe  $ROOT_DIR/dist/vdam_windows_amd64/vtools.exe
chmod +x  $ROOT_DIR/dist/vdam_linux_arm64/vtools
chmod +x $ROOT_DIR/dist/vdam_linux_amd64/vtools
#chmod +x $ROOT_DIR/dist/vdam_darwin_amd64/vtools
#chmod +x $ROOT_DIR/dist/vdam_darwin_arm64/vtools


echo "copy config files "
cp -r $PACKAGE_DIR/vtools.yaml  $ROOT_DIR/dist/vdam_linux_arm64/configs/
cp -r $PACKAGE_DIR/vtools.yaml  $ROOT_DIR/dist/vdam_linux_amd64/configs/
#cp -r $PACKAGE_DIR/vtools.yaml  $ROOT_DIR/dist/vdam_darwin_amd64/configs/
#cp -r $PACKAGE_DIR/vtools.yaml  $ROOT_DIR/dist/vdam_darwin_arm64/configs/
#cp -r $PACKAGE_DIR/vtools.yaml  $ROOT_DIR/dist/vdam_windows_amd64/configs/




# 三、编译 dcs
echo "3.build............................................java"
cd ${ROOT_DIR}
mvn clean package

cp proxy/target/proxy*.jar  $ROOT_DIR/dist/vdam_linux_arm64/proxy.jar
cp proxy/target/proxy*.jar  $ROOT_DIR/dist/vdam_linux_amd64/proxy.jar
#cp proxy/target/proxy*.jar  $ROOT_DIR/dist/vdam_darwin_amd64/proxy.jar
#cp proxy/target/proxy*.jar  $ROOT_DIR/dist/vdam_darwin_arm64/proxy.jar
#cp proxy/target/proxy*.jar  $ROOT_DIR/dist/vdam_windows_amd64/proxy.jar


cp dps/target/dps*.jar  $ROOT_DIR/dist/vdam_linux_arm64/dps.jar
cp dps/target/dps*.jar  $ROOT_DIR/dist/vdam_linux_amd64/dps.jar
#cp dps/target/dps*.jar  $ROOT_DIR/dist/vdam_darwin_amd64/dps.jar
#cp dps/target/dps*.jar  $ROOT_DIR/dist/vdam_darwin_arm64/dps.jar
#cp dps/target/dps*.jar  $ROOT_DIR/dist/vdam_windows_amd64/dps.jar


cp dcs/target/dcs*.jar  $ROOT_DIR/dist/vdam_linux_arm64/dcs.jar
cp dcs/target/dcs*.jar  $ROOT_DIR/dist/vdam_linux_amd64/dcs.jar
#cp dcs/target/dcs*.jar  $ROOT_DIR/dist/vdam_darwin_amd64/dcs.jar
#cp dcs/target/dcs*.jar  $ROOT_DIR/dist/vdam_darwin_arm64/dcs.jar
#cp dcs/target/dcs*.jar  $ROOT_DIR/dist/vdam_windows_amd64/dcs.jar

cd $ROOT_DIR/dist
tar -czvf vdam_linux_arm64.${branch}.${commitId}.${buildDate1}.tar.gz vdam_linux_arm64
#tar -czvf vdam_linux_amd64.${branch}.${commitId}.${buildDate1}.tar.gz vdam_linux_amd64
#tar -czvf vdam_darwin_amd64.${branch}.${commitId}.${buildDate1}.tar.gz vdam_darwin_amd64
#tar -czvf vdam_darwin_arm64.${branch}.${commitId}.${buildDate1}.tar.gz vdam_darwin_arm64
#tar -czvf vdam_windows_amd64.${branch}.${commitId}.${buildDate1}.tar.gz vdam_windows_amd64


